package cn.net.yugu.doraemon.common.util;

import java.time.Instant;
import java.util.Date;
import java.util.Optional;
import java.util.function.Function;


/**
 * Instant 相关
 *
 * @author xiangzhurui
 */
public class InstantUtils {

    private InstantUtils() {
    }

    public static Long toEpochMilli(Instant instant) {
        return Optional.ofNullable(instant)
                .map(Instant::toEpochMilli)
                .orElse(null);
    }

    public static Instant ofEpochMilli(Long epochMilli) {
        return getInstant(epochMilli, Instant::ofEpochMilli);

    }

    public static Instant ofEpochSecond(Long epochSecond) {
        return getInstant(epochSecond, Instant::ofEpochSecond);
    }

    public static Instant ofDate(Date date) {
        return getInstant(date, Date::toInstant);
    }

    public static <T, F extends Function<T, Instant>, R> Instant getInstant(T data, F func) {
        return Optional.ofNullable(data)
                .map(func)
                .orElse(null);
    }

}
